﻿using System;
using System.Collections.Generic;
using System.Linq;
using HtmlAgilityPack;
using Tisonet.Dobrak.Domain.Models;

namespace Tisonet.Dobrak.Domain.Services.Projections
{
    public class DobrakHtmlParser
    {
        private readonly HtmlNode _html;

        public DobrakHtmlParser(HtmlNode html)
        {
            if (html == null)
            {
                throw new ArgumentNullException("html");
            }

            _html = html;
        }

        public IList<Projection> ParseProjections()
        {
            var projections = new List<Projection>();

            var projectionsNodes = _html.SelectNodes("//input[@class='Datum_cas']") ?? Enumerable.Empty<HtmlNode>();

            foreach (var projectionNode in projectionsNodes)
            {
                if (IsValidProjectionElement(projectionNode))
                {
                    Projection projection = CreateProjection(projectionNode);

                    if (projection != null)
                    {
                        projections.Add(projection);
                    }
                }
            }

            return projections;
        }

        private static bool IsValidProjectionElement(HtmlNode projectionNode)
        {
            DateTime test;
            return projectionNode.HasAttributes 
                && DateTime.TryParse(projectionNode.Attributes["ID"].Value, out test) 
                && projectionNode.NextSibling != null
                && projectionNode.NextSibling.Attributes["rel"] != null
                && projectionNode.PreviousSibling != null;
        }

        private static Projection CreateProjection(HtmlNode projectionNode)
        {
            var projection = new Projection();
            
            projection.ID = projectionNode.NextSibling.Attributes["rel"].Value;
         
            projection.Name = projectionNode.NextSibling.InnerHtml.Trim().Replace("<br>", Environment.NewLine);

            projection.Date = DateTime.Parse(projectionNode.Attributes["ID"].Value);

            TimeSpan time = ParseProjectionTime(projectionNode);

            projection.Date = projection.Date.Add(time);
           
       
            return projection;

        }

        private static TimeSpan ParseProjectionTime(HtmlNode projectionNode)
        {
            TimeSpan time = TimeSpan.Zero;
            var timeParts = projectionNode.PreviousSibling.InnerText.Split(new[] {','}, StringSplitOptions.RemoveEmptyEntries);

            if (timeParts.Length == 2)
            {
                if (!TimeSpan.TryParse(timeParts[1].Replace(".", ":"), out time))
                {
                    time = TimeSpan.Zero;
                }
            }

            return time;
        }

        public Movie ParseMovie()
        {
            Movie movie = new Movie();

            movie.OriginalName = GetInnerTextOfElement(_html.SelectSingleNode("//span[@class='ajax_box Orig_nazev']"));
            movie.TechInfo = GetInnerTextOfElement(_html.SelectSingleNode("//span[@class='Tech_info']"));
            movie.Director = GetInnerTextOfElement(_html.SelectSingleNode("//span[@class='Rezie']"));
            movie.Players = GetInnerTextOfElement(_html.SelectSingleNode("//span[@class='Hraji']"));
            movie.Description = GetInnerTextOfElement(_html.SelectSingleNode("//span[@class='Popis']"));

            return movie;
        }

        private string GetInnerTextOfElement(HtmlNode htmlNode)
        {
            if (htmlNode != null)
            {
                return htmlNode.InnerText.Trim();
            }

            return string.Empty;
        }
    }
}
